package com.judian.smart.receiver;
//joe 18_0919

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.judian.smart.activity.MainActivity;
import com.judian.smart.base.SocketMainResult;
import com.judian.smart.interfaces.AnHongListener;
import com.judian.smart.interfaces.BlueToothListener;
import com.judian.smart.interfaces.DownListener;
import com.judian.smart.interfaces.GateWayListener;
import com.judian.smart.interfaces.GetDeviceListener;
import com.judian.smart.interfaces.HostListener;
import com.judian.smart.interfaces.MachineCode;
import com.judian.smart.interfaces.RemoteControlListener;
import com.judian.smart.interfaces.SecurityListener;
import com.judian.smart.model.AnHong;
import com.judian.smart.model.BlueDevice;
import com.judian.smart.model.Device;
import com.judian.smart.model.GateWay;
import com.judian.smart.model.Master;
import com.judian.smart.model.Mode;
import com.judian.smart.model.ResponseInfo;
import com.judian.smart.model.SubscribeInfo;
import com.judian.smart.model.VoiceInfo;
import com.judian.smart.net.TcpSender;
import com.judian.smart.netty.NettySocketClient;
import com.judian.smart.service.BroadcastService;
import com.judian.smart.service.GateWayService;
import com.judian.smart.socket.ISocketCode;
import com.judian.smart.util.AppTools;
import com.judian.smart.util.JSONHelper;
import com.judian.smart.util.MasterUtils;
import com.judian.smart.util.ProjectUtils;

import org.json.JSONObject;

import java.util.List;

public class ReceiverBroadcast extends BroadcastReceiver {
    public static GateWayListener listener;

    public static void setListener(GateWayListener listener) {
        ReceiverBroadcast.listener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        //Peter 20191018 小可放置一天后根据BugLy发现ReceiverBroadcast发出ANR的错误
        //说明可能这里面有耗时的操作 这里开启个服务代替这里所有逻辑
        intent.setClass(context, BroadcastService.class);
        context.startService(intent);
    }

}









































//修改之前的代码备份
   /* private String K_SERVICE = "Service";
    private static GateWayListener listener;
    private static AnHongListener anHongListener;

    public static void setSecurityListener(SecurityListener securityListener) {
        ReceiverBroadcast.securityListener = securityListener;
    }

    private static SecurityListener securityListener;

    public static void setAnHongListener(AnHongListener anHongListener) {
        ReceiverBroadcast.anHongListener = anHongListener;
    }

    private static BlueToothListener blueToothListener;

    *//**
     * @param listener the listener to set
     *//*
    public static void setListener(GateWayListener listener) {
        ReceiverBroadcast.listener = listener;
    }

    private static GetDeviceListener getDeviceListener;

    private static DownListener downListener;

    private static MachineCode machineCode;
    private static HostListener hostListener;
    private static RemoteControlListener remoteControlListener;

    public static void setMachineCode(MachineCode machineCode) {
        ReceiverBroadcast.machineCode = machineCode;
    }

    *//**
     * @param downListener the downListener to set
     *//*
    public static void setDownListener(DownListener downListener) {
        ReceiverBroadcast.downListener = downListener;
    }

    *//**
     * @param getDeviceListener the getDeviceListener to set
     *//*
    public static void setGetDeviceListener(GetDeviceListener getDeviceListener) {
        ReceiverBroadcast.getDeviceListener = getDeviceListener;
    }

    public static void setBlueToothListener(BlueToothListener blueToothListener) {
        ReceiverBroadcast.blueToothListener = blueToothListener;
    }

    public static void setHostListener(HostListener hostListener) {
        ReceiverBroadcast.hostListener = hostListener;
    }

    public static void setRemoteControlListener(RemoteControlListener remoteControlListener) {
        ReceiverBroadcast.remoteControlListener = remoteControlListener;
    }*/

//修改之前的代码备份
/*        try {
            String result = intent.getExtras().getString("str");
            Log.d("ReceiveBrodcast收到广播回码", result);
            ResponseInfo info = JSONHelper.parseObject(result, ResponseInfo.class);
            String service = info.getMsgType();
            if (!TextUtils.isEmpty(service)) {
                SocketMainResult.ServiceType serviceType = SocketMainResult.getServiceType(service);
                switch (serviceType) {
                    case LOGIN:
                        if (info.getMsgState() == 2) {
                            // YYX: 2018/11/7 取消弹出登录成功的提示框
                            ProjectUtils.getRootPath().setControlState(2);
                        } else if (info.getMsgState() == 1) {
                            ProjectUtils.getRootPath().setControlState(0);
                        }
                        Log.e("NetCmd", AppTools.getCurrentTime() + "________nettyLogin");
                        break;
                    case LINE:
                        if (info.getMsgState() == 1) {
                            Log.e("NetCmd", AppTools.getCurrentTime() + "________nettyLine");
                        } else {
//                                showToast(context.getString(R.string.login_fail));
                        }
                        break;
                    case CONNECT:
                        if (listener != null) {
                            String toID = info.getToID();
                            if (GateWayService.mp.containsKey(toID)) {
                                String id = GateWayService.mp.get(toID);
                                int state = info.getMsgState();
                                listener.receiveGateWayStatus(1, Integer.parseInt(id), state);
                                context.sendBroadcast(new Intent("GateWayIsLineReceiver").putExtra("gateway_connect", true));
                                Log.e("BCameraID", "设备ID:" + toID);
                            }
                        }
                        Log.e("NetCmd", AppTools.getCurrentTime() + "________nettyConn");
                        break;

                    case GETANHONGINFO: {
                        // 1:火警1
                        String[] results = info.getContent().split(":");
                        if (results.length == 3) {
                            AnHong anHong = new AnHong();
                            anHong.setId(Integer.parseInt(results[0]));

                            anHong.setMsgName(results[1]);
                            anHong.setMsgInfo(results[2]);
                            if (anHongListener != null) {
                                anHongListener.getAnHong(anHong);
                            }
                        }
                    }
                    break;
                    case FORWARD:
                        if (info.getMsgState() == 1) {
//                                showToast(info.getToID() + "网关" + context.getString(R.string.line));
                        } else {
//                                showToast(info.getToID() + context.getString(R.string.gateWayUnLine));

                            List<GateWay> list = GateWayService.list;
                            if (list != null) {
                                for (int i = 0; i < list.size(); i++) {
                                    final GateWay gateWay = list.get(i);
                                    if (gateWay.getGatewayID().equals(
                                            info.getToID())) {
                                        String result1 = ISocketCode
                                                .setConnect(
                                                        gateWay.getGatewayID(),
                                                        gateWay.getUserName(),
                                                        gateWay.getUserPassWord(),
                                                        gateWay.getIsCurrent());
//                                        MainActivity.mInstance.sendCode(result1);
                                        NettySocketClient.getInstans().sendData(result1);
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    case RESPONSE:
                        //判斷是否當前网關列表
                        List<GateWay> gateWayList = GateWayService.list;
                        for (int i = 0; i < gateWayList.size(); i++) {
                            GateWay gateway = gateWayList.get(i);
                            int gatewayTypeID = gateway.getTypeId();
//                            if (SystemInfo.getSSID(MyApplication.getInstance()).startsWith("RAK") && gatewayTypeID == 5) {
//                                TcpSender.dataProcess(info.getContent());
//                                break;
//                            } else if (gateway.getGatewayID().equals(info.getToID()) && gatewayTypeID != 3) {
//                                TcpSender.dataProcess(info.getContent());
//                                break;
//                            }
                            if (gateway.getGatewayID().equals(info.getToID()) && gatewayTypeID != 3) {
                                TcpSender.dataProcess(info.getContent());
                                break;
                            }
                        }
//                        TcpSender.dataProcess(info.getContent());
                        if (machineCode != null) {
                            machineCode.setCode(info);
                        }
                        break;
                    case REMOTEMUSIC:
                        //info.getContent()
*//*                        Intent aiuiIntent = new Intent(context, WakeUpAIUIService.class);
                        aiuiIntent.putExtra("conveyVoiceText", info.getContent());
                        context.startService(aiuiIntent);*//*
                        break;
                    case ERROR:
                        MainActivity.mInstance.freshFindDevice();
                        break;
                    case GETMODELIST:
                        String result1 = info.getContent();
                        if (getDeviceListener != null) {
                            getDeviceListener.getModeList(result1);
                        }
                        break;
                    case GETDEVICEINFO: {
                        Log.d("infoDevice", info.getContent());//主机没有场景
                        String results = info.getContent();
                        try {
                            Device device = JSONHelper.parseObject(results, Device.class);

                            if (device != null) {
                                if (getDeviceListener != null) {
                                    getDeviceListener.getDevice(device);
                                }
                            } else {
                                if (getDeviceListener != null) {
                                    getDeviceListener.getMessage(results);
                                }
                            }
                        } catch (Exception e) {
                            if (getDeviceListener != null) {
                                getDeviceListener.getMessage(results);
                            }
                        }
                    }
                    break;
                    case GETMODEINFO: {
                        String results = info.getContent();
                        try {
                            Mode mode = JSONHelper.parseObject(results, Mode.class);
                            if (mode != null) {
                                if (getDeviceListener != null) {
                                    getDeviceListener.getMode(mode);
                                }
                            } else {
                                if (getDeviceListener != null) {
                                    getDeviceListener.getMessage(results);
                                }
                            }
                        } catch (Exception e) {
                            if (getDeviceListener != null) {
                                getDeviceListener.getMessage(results);
                            }
                        }
                    }
                    break;
                    case DOWNINFO:
                        if (downListener != null) {
                            if (AppTools.isNumeric(info
                                    .getContent())) {
                                downListener.count(info
                                        .getContent());
                            }
                        }
                        break;

                    case APKINFO:
                        if (downListener != null) {
                            downListener.count(info
                                    .getContent());
                        }

                        break;
                    case GETMASTERINFO:
                        if (machineCode != null) {
                            machineCode.setCode(info);
                        }
                        break;
                    case GETVOICEINFO: {
                        String[] voices = info.getContent().split(";");
                        if (voices.length == 5) {
                            VoiceInfo voice = new VoiceInfo();
                            voice.setId(Integer.parseInt(voices[0]));
                            voice.setVoiceName(voices[1]);
                            voice.setVoiceAnswer(voices[2]);
                            voice.setVoiceAction(voices[3]);
                            voice.setUserName(voices[4]);
                            if (getDeviceListener != null) {
                                getDeviceListener.getVoice(voice);
                            }
                        } else {
                            if (getDeviceListener != null) {
                                getDeviceListener.getMessage(info.getContent());
                            }
                        }
                    }
                    break;
                    case GETSUBSCRIBEINFO:
                        String[] voices = info.getContent().split(";");
                        if (voices.length == 7) {
                            SubscribeInfo voice = new SubscribeInfo();
                            voice.setId(Integer.parseInt(voices[0]));
                            voice.setSubscribeName(voices[1]);
                            voice.setSubscribeDate(voices[2]);
                            voice.setSubscribeTime(voices[3]);
                            voice.setSubscribeEvent(voices[4]);
                            voice.setSubscribeAction(voices[5]);
                            voice.setUserName(voices[6]);
                            if (getDeviceListener != null) {
                                getDeviceListener.getSubscribe(voice);
                            }
                        } else {
                            if (getDeviceListener != null) {
                                getDeviceListener.getMessage(info.getContent());
                            }
                        }
                        break;
                    case GETWIFIINFO:
                        JSONObject jsonObject = new JSONObject(info.getContent());
                        Master master = new Master();
                        master.setMn(jsonObject.getString("MasterName"));
                        master.setMc(jsonObject.getString("MasterMac"));
                        master.setWf(jsonObject.getString("MasterWiFi"));
                        MasterUtils.addDevice(master);
                        break;
                    case SECURITYINFO:
                        if (securityListener != null) {
                            securityListener.getSecurity(info.getContent());
                        }
                        break;
                    case GETBLUETOOTHINFO:
                        String[] content = info.getContent().split(":");
                        switch (content[0]) {
                            case "device":
                                if (blueToothListener != null) {
                                    BlueDevice blueDevice = new BlueDevice();
                                    blueDevice.setDeviceName(content[1]);
                                    blueDevice.setAddress(content[2]);
                                    blueDevice.setDeviceType(Integer.parseInt(content[3]));
                                    blueDevice.setBoundState(Integer.parseInt(content[4]));
                                    blueDevice.setConnectState(0);
                                    blueToothListener.SearchDevice(blueDevice);
                                }
                                break;
                            case "connect":
                                if (blueToothListener != null) {
                                    blueToothListener.OnConnectState(content[1], Integer.parseInt(content[2]));
                                }
                                break;
                            case "bound":
                                if (blueToothListener != null) {
                                    blueToothListener.OnBondState(content[1], Integer.parseInt(content[2]));
                                }
                                break;
                        }
                        break;
                    case GETGATEWAYINFO:
                        if (hostListener != null) {
                            hostListener.getMsg(info.getContent());
                        }
                        break;
                    case REMOTECONTROLINFO:
                        if (remoteControlListener != null) {
                            remoteControlListener.setMsg(info.getMasterType(), info.getContent());
                        }
                        break;
                    default:
                        break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }*/